package com.tbit.uqbike.webmanager.util;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author chenmengran
 * @Description: 读取资源文件
 * @date 2019\12\14 001417:05
 */
@Slf4j
public class ConfigUtil {
    private static InputStream input;
    //弄成单例的
    private static ConfigUtil config;
    private volatile Properties configuration = new Properties();

    //构造函数内完成了加载配置文件
    private ConfigUtil(String path) {
        input = this.getClass().getResourceAsStream(path);
        this.configuration.clear();
        try {
            this.configuration.load(input);
        } catch (IOException e) {
            System.out.println(e.getMessage());
            log.error(e.getMessage(), e);
        }
    }

    public static ConfigUtil getInstance(String path) {
        if (config == null) {
            config = new ConfigUtil(path);
        }
        return config;
    }

    public static void main(String[] args) throws IOException {
        String path = "/properties/baseParams.properties";
        System.out.println(getInstance(path).getStringValue("franchisee.name"));
    }

    public String getStringValue(String key) throws IOException {
        return this.configuration.getProperty(key);
    }

    public String getStringValue(String key, String defaultValue) {
        String value = this.configuration.getProperty(key);
        if (value == null) {
            return defaultValue;
        } else {
            return value;
        }
    }

    public void setConfiguration(String key, String value) {
        this.configuration.setProperty(key, value);
    }
}
